import { defHttp } from "/@/utils/http/axios"
import { Modal } from "ant-design-vue"
import { RequestHeaderEnum } from "/@/enums/httpEnum"
enum Api {
  page = "/food-service/goods/page",
  get = "/food-service/goods/get",
  save = "/food-service/goods/save",
  edit = "/food-service/goods/update",
  delete = "/food-service/goods/delete",
  deleteBatch = "/food-service/goods/deleteBatch",
  updatePublishStatus = "/food-service/goods/updatePublishStatus",
  updateStoreGoodsPublishStatus = "/food-service/goods/updateStoreGoodsPublishStatus",
  batchAddIntoCurrentStore = "/food-service/goods/batchAddIntoCurrentStore",
  batchDeleteFromCurrentStore = "/food-service/goods/batchDeleteFromCurrentStore",
  pageStoreGoods = "/food-service/goods/pageStoreGoods",
}

export const list = (params) => defHttp.get({ url: Api.page, params })
export const pageStoreGoods = (params) => {
  const headers = {}
  headers[RequestHeaderEnum.TENANT_ID] = params.tenantId
  return defHttp.get({
    url: Api.pageStoreGoods,
    params,
    headers,
  })
}
export const getGoods = (params) => defHttp.get({ url: Api.get, params })

export const deleteGoods = (params, handleSuccess) => {
  return defHttp.delete({ url: Api.delete, params }, { joinParamsToUrl: true }).then(() => {
    handleSuccess()
  })
}

export const batchdeleteGoods = (params, handleSuccess) => {
  Modal.confirm({
    title: "确认删除",
    content: "是否删除选中数据",
    okText: "确认",
    cancelText: "取消",
    onOk: () => {
      return defHttp.delete({ url: Api.deleteBatch, data: params }, { joinParamsToUrl: true }).then(() => {
        handleSuccess()
      })
    },
  })
}

export const saveOrUpdateGoods = (params, isUpdate) => {
  const url = isUpdate ? Api.edit : Api.save
  return defHttp.post({ url: url, params })
}

export const updatePublishStatus = (params) => {
  return defHttp.post({ url: Api.updatePublishStatus, params }, { joinParamsToUrl: true })
}

/**
 * 更新门店商品发布状态
 */
export const updateStoreGoodsPublishStatus = (params) => {
  return defHttp.post({ url: Api.updateStoreGoodsPublishStatus, params }, { joinParamsToUrl: true })
}

export const batchAddIntoCurrentStore = (params) => {
  return defHttp.post({ url: Api.batchAddIntoCurrentStore, params }, { joinParamsToUrl: true })
}

export const deleteFromCurrentStore = (params, handleSuccess) => {
  return defHttp.delete({ url: Api.batchDeleteFromCurrentStore, params }, { joinParamsToUrl: true }).then(() => {
    handleSuccess()
  })
}

export const batchDeleteFromCurrentStore = (params, handleSuccess) => {
  Modal.confirm({
    title: "确认删除",
    content: "是否删除选中数据",
    okText: "确认",
    cancelText: "取消",
    onOk: () => {
      return defHttp.delete({ url: Api.batchDeleteFromCurrentStore, data: params }, { joinParamsToUrl: true }).then(() => {
        handleSuccess()
      })
    },
  })
}
